//
//  JZHHFView.m
//  极致荟
//
//  Created by hetao on 14-9-3.
//  Copyright (c) 2014年 mac. All rights reserved.
//

#import "JZHHFView.h"

@implementation JZHHFView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

+ (id)share
{
    static JZHHFView *jzh;
    if(!jzh)
    {
        jzh=[[NSBundle mainBundle] loadNibNamed:@"JZHHFView" owner:self options:nil][0];
        [[NSNotificationCenter defaultCenter] addObserver:jzh selector:@selector(not:) name:UIKeyboardWillChangeFrameNotification object:nil];
        [jzh addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:jzh action:@selector(top:)]];
        [jzh.content setReturnKeyType:UIReturnKeyDone];
    }
    
    return jzh;
}

- (void)not:(NSNotification *)not
{
    CGPoint bpoint=[[[not userInfo] objectForKey:UIKeyboardCenterBeginUserInfoKey] CGPointValue];
    CGPoint epoint=[[[not userInfo] objectForKey:UIKeyboardCenterEndUserInfoKey] CGPointValue];
    [UIView animateWithDuration:0.3 animations:^{
        [self.con.view setCenter:CGPointMake(self.con.view.center.x, self.con.view.center.y+(epoint.y-bpoint.y))];
         [self setCenter:CGPointMake(self.center.x, self.center.y+(epoint.y-bpoint.y))];
    } completion:^(BOOL finished) {
        if(![self.content isFirstResponder])
        {
            self.hidden=YES;
        }
    }];
}

- (void)top:(UITapGestureRecognizer *)tap
{
    [self hideeHFview];
}
- (void)showHFview:(UIViewController *)controller
{
    self.hidden=NO;
    self.con=controller;
    self.content.delegate=self;
    
    [self setFrame:CGRectMake(0,0, self.frame.size.width, HEIGHT)];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self.content becomeFirstResponder];
    
}
- (void)hideeHFview
{
    [self.content resignFirstResponder];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    if([text isEqualToString:@"\n"])
    {
        if(textView.text.length!=0)
        {
            self.okContenr(textView.text);
            textView.text=@"";
            self.tslable.hidden=YES;
        }
        [self hideeHFview];
        return NO;
    }
    return YES;
}
- (void)textViewDidChangeSelection:(UITextView *)textView
{
    if (textView.text.length==0)
    {
        self.tslable.hidden=NO;
    }else
    {
        self.tslable.hidden=YES;
    }
}


@end
